package logic

import (
	"context"
	"fmt"
	"novel/models/mysql"

	"novel/novelrpc/internal/svc"
	"novel/novelrpc/novelrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type RegisterLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRegisterLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RegisterLogic {
	return &RegisterLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RegisterLogic) Register(in *novelrpc.RegisterRequest) (*novelrpc.RegisterResponse, error) {
	// todo: add your logic here and delete this line
	var user mysql.User
	mysql.DB.Debug().Where("account=?", in.Account).Find(&user)
	if user.ID != 0 {
		return nil, fmt.Errorf("当前账号未登录，请前去注册")
	}
	use := mysql.User{
		Account:  in.Account,
		Password: in.Password,
	}
	mysql.DB.Debug().Create(&use)
	return &novelrpc.RegisterResponse{
		UserId: int64(use.ID),
	}, nil
}
